//
//  RotationLoopProgressView.swift
//  FromWatchToCircle
//
//  Created by 张海峰 on 2018/6/1.
//  Copyright © 2018年 张海峰. All rights reserved.
//
/*尊重OC原创作者：高少东 https://github.com/gsdios/SDProgressView.git */
/*写该demo的目的：主要是想从时钟开始，一步步窥见项目中和圆有关的东西，从而学习巩固，加深理解。
 (项目不定时更新，所以最好从下面地址获取最新demo),感觉我这个demo对你有启发或者帮助，欢迎fork 感谢star
 https://github.com/FighterLightning/FromWatchToCircle.git
 */
import UIKit
let defalutText: String =  "LOADING..."
class RotationLoopProgressView: BaseProgressView {
    var angleInterval:CGFloat = 0
    func Start(){
        let timer:Timer = Timer.scheduledTimer(timeInterval: 0.03, target: self, selector: #selector(changeAngle), userInfo: nil, repeats: true)
        RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)
    }
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        let ctx: CGContext = UIGraphicsGetCurrentContext()!
        // 背景圆
        let centerXY: CGFloat = defaultWH/2
        let radius :CGFloat = centerXY - ProgressViewItemMargin
        // 进程圆1
        UIColor.yellow.set()// 进度条颜色
        ctx.setLineWidth(4)
        let endAngle: CGFloat = CGFloat(-Double.pi*3.8/2) + angleInterval ; // 初始值0.05
        ctx.addArc(center: CGPoint.init(x: centerXY, y: centerXY), radius: radius, startAngle: angleInterval, endAngle: endAngle, clockwise: true)
        ctx.strokePath()
        // 进程圆2
        UIColor.red.set()// 进度条颜色
        ctx.setLineWidth(4)
        let endAngle2: CGFloat = CGFloat(Double.pi*3.8/2) - angleInterval ; // 初始值0.05
        ctx.addArc(center: CGPoint.init(x: centerXY, y: centerXY), radius: radius*6/7, startAngle:endAngle2 , endAngle: -angleInterval , clockwise: true)
        ctx.strokePath()
        // 进程圆3
        UIColor.yellow.set()// 进度条颜色
        ctx.setLineWidth(4)
        let endAngle3: CGFloat = CGFloat(-Double.pi*3.8/2) + angleInterval ; // 初始值0.05
        ctx.addArc(center: CGPoint.init(x: centerXY, y: centerXY), radius: radius*3/4, startAngle: angleInterval, endAngle: endAngle3, clockwise: true)
        ctx.strokePath()
        //显示进度数字
        //文字样式属性
        let style = NSMutableParagraphStyle()
        style.alignment = .center
        let attributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: defaultTextFont),
                          NSAttributedStringKey.foregroundColor: UIColor.white,
                          NSAttributedStringKey.paragraphStyle: style]
        self.setCenterProgressText(text: defalutText, attributes: attributes)
    }
    @objc func changeAngle(){
        angleInterval += .pi * 0.1
        if angleInterval >= .pi * 2 {
            angleInterval = 0;
        }
        self.setNeedsDisplay()
    }
}
